package com.goparry.taskman.aws;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

import com.goparry.taskman.GLOBAL;
import com.goparry.taskman.TaskManUtils;
import com.goparry.taskman.repository.TaskNote;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;

import android.net.Uri;
import android.os.AsyncTask;

public class AsyncAWSGetNote extends AsyncTask<TaskNote, Integer, Uri> {
	ProgressDialog mProgressDialog;
	Context mContext;
	String mimeType = "";
	
	public AsyncAWSGetNote() {
		mContext = null;		
	}
	public AsyncAWSGetNote(Context context) {
		mContext = context;		
	}

	@Override
	protected void onPreExecute() {
//		mProgressDialog.setMax(fileLength);
		if (mContext != null) {
	        mProgressDialog= new ProgressDialog(mContext);
	        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
	        mProgressDialog.setMessage("Loading task data...");
	        mProgressDialog.setIndeterminate(false);
			mProgressDialog.show();
		}
		super.onPreExecute();
	}
	@Override
	protected Uri doInBackground(TaskNote... arg0) {
		TaskNote taskNote = arg0[0];
		publishProgress(0,(int)taskNote.getLength());
		String type = taskNote.getType();
		
		String extension = "";
		if (type.equalsIgnoreCase("text")) {
			mimeType = "text/plain";
			extension = ".txt";
		} else if (type.equalsIgnoreCase("voice")) {
			mimeType = "text/plain";
			extension = ".avi";
		} else if (type.equalsIgnoreCase("picture")) {
			mimeType = "image/jpeg";
			extension = ".jpg";
		} else if (type.equalsIgnoreCase("video")) {
			mimeType = "video/mp4";
			extension = ".mp4";
		}
		InputStream inputStream = AWSUtils.getTaskNoteFile(taskNote.getKey(), extension);
		
		String path = mContext.getFilesDir().getAbsolutePath();
		String fileName = UUID.randomUUID() + extension;
		File file = new File(path, fileName);
		Uri uri = Uri.parse(GLOBAL.CONTENT_PATH + fileName);
        byte[] buf = new byte[1024];
        int len;
        long bytesRead=0;
		try {
			file.createNewFile();
			OutputStream outputStream= new FileOutputStream(file);
			while ((len = inputStream.read(buf)) > 0) {
				outputStream.write(buf,0,len);
				bytesRead += len;
				publishProgress((int)bytesRead);
			}
			outputStream.close();
			inputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}       
		return uri;
	}
	@Override
	protected void onProgressUpdate(Integer... values) {
		if (values.length>1) {
			mProgressDialog.setMax(values[1]);
		}
		mProgressDialog.setProgress(values[0]);
	}
	@Override
	protected void onPostExecute(Uri uri) {
		mProgressDialog.dismiss();
		if (TaskManUtils.isIntentAvailable(mContext, android.content.Intent.ACTION_VIEW)) {
			Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
			intent.setDataAndType(uri, mimeType);
			mContext.startActivity(intent);
		} else {
		
		}
		
	}


}

